﻿using Roslyn.Compilers.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QInjection.Injector
{
    internal class NodeToTypeMap
    {
        private static volatile NodeToTypeMap instance;
        private static object syncRoot = new Object();

        private Dictionary<Node, Type> _map;

        internal NodeToTypeMap()
        {
            _map = new Dictionary<Node, Type>();
            _map[Node.SimpleLambdaExpression] = typeof(SimpleLambdaExpressionSyntax);
            _map[Node.Parameter] = typeof(ParameterSyntax);
            _map[Node.BinaryExpression] = typeof(BinaryExpressionSyntax);
            _map[Node.PostfixUnaryExpression] = typeof(PostfixUnaryExpressionSyntax);
            _map[Node.PrefixUnaryExpression] = typeof(PrefixUnaryExpressionSyntax);
            _map[Node.ConditionalExpression] = typeof(ConditionalExpressionSyntax);
            _map[Node.ParenthesizedExpression] = typeof(ParenthesizedExpressionSyntax);
            _map[Node.InvocationExpression] = typeof(InvocationExpressionSyntax);
            _map[Node.Argument] = typeof(ArgumentSyntax);
            _map[Node.MemberAccessExpression] = typeof(MemberAccessExpressionSyntax);
            _map[Node.IdentifierName] = typeof(IdentifierNameSyntax);
            _map[Node.LiteralExpression] = typeof(LiteralExpressionSyntax);
        }

        internal static NodeToTypeMap Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new NodeToTypeMap();
                    }
                }

                return instance;
            }
        }

        internal Type this[Node index]
        {
            get
            {
                if (instance == null)
                    throw new Exception("Singleton NodeToType not instanciated");
                return instance._map[index];
            }
        }
    }
}
